package org.springcloud.wisdom.spring_02_classroom.admin.managestudent.service;

import org.springcloud.wisdom.spring_02_classroom.admin.managestudent.dao.ManageStudentDao;
import org.springcloud.wisdom.spring_02_classroom.student.sschedule.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springcloud.wisdom.spring_02_classroom.bean.Page;

import java.util.List;

@Service
public class ManageStudentService {
    @Autowired
    ManageStudentDao manageStudentDao;

    //修改学生密码
    public boolean updateStudentPassword(Student student) {
        if (student == null){
            return false;
        }if (student.getSid() == null || student.getSid().trim().length() == 0){
            return false;
        }if (student.getAdpass() == null || student.getAdpass().trim().length() == 0){
            return false;
        }
        return manageStudentDao.updateStudentPassword(student) >0;
    }

    //基于课模糊查询学号和姓名的分页查看所有学生信息
    public Page<Student> AllStudent(int pageNum, int pageSize, String sid ,String sname ){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Student> list= manageStudentDao.get(start,pageSize,sid,sname);
        //获取总条目数
        int total= manageStudentDao.CountStudents(sid,sname);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Student> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
}
